package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.QuotationService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/quotation")
public class QuotationController {
    @Autowired
    QuotationService quotationService;

    /**
     * 报价单分页查询
     * @param search
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/quotation-list")
    @ResponseBody
    public Map<String,Object> selectProduct(String search,Integer pageNum, Integer pageSize, HttpSession session){
        Page<Object> obj = PageHelper.startPage(pageNum,pageSize);
        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        String empName = null;
        if(jueNameList.indexOf("boss")>-1 ||jueNameList.indexOf("销售经理")>-1  ){
            empName = null;
        }else{
            empName = us.getEmpName();
        }
        quotationService.selectQuotation(search,empName);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        return map;
    }


    /**
     * 新增报价单和报价详情
     * @param details
     */
    @RequestMapping("/addQuotation")
    @ResponseBody
    public void addQuotation(String details){
        JSONObject obj = (JSONObject)JSONObject.parse(details);
        List detailss = obj.getObject("pur",List.class);
        Quotation quotation = obj.getObject("quotation",Quotation.class);
        System.out.println("报价单"+quotation);
        System.out.println("报价单2"+detailss);
        quotationService.addQuotation(detailss,quotation);
    }

    /**
     * 修改报价单
     * @param quotationId
     * @return
     */
    @RequestMapping("/upQuotation")
    @ResponseBody
    public List<Quotation> upQuotation(Integer quotationId){
        return quotationService.upQuotation(quotationId);
    }

    /**
     * 查询需要修改的报价详情
     * @param quotationId
     * @return
     */
    @RequestMapping("/quotationDetails-list")
    @ResponseBody
    public List<Quotationdetails> quotationDetailsList(Integer quotationId){
        return quotationService.quotationDetailsList(quotationId);
    }

    /**
     * 删除报价单
     * @param quotationId
     */
    @RequestMapping("/delQuotation")
    @ResponseBody
    public void delQuotation(Integer quotationId){
        quotationService.delQuotation(quotationId);
    }

    /**
     * 查询单号
     * @return
     */
    @RequestMapping("/selectOdd")
    @ResponseBody
    public String delQuotation(){
        System.out.println(quotationService.selectOdd());
        return quotationService.selectOdd();
    }

    /**
     * 报价客户列表
     * @param khsearch
     * @return
     */
    @RequestMapping("/kh-list")
    @ResponseBody
    public Map<String,Object> selectKh(String khsearch,Integer pageNum, Integer pageSize,String empName){
        Page<Object> obj = PageHelper.startPage(pageNum,pageSize);
        quotationService.selectKh(khsearch,empName);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        return map;
    }

    /**
     * 客户联系人列表
     * @param khName
     * @return
     */
    @RequestMapping("/lxr-list")
    @ResponseBody
    public List<Contacts> selectLxr(String khName){
        return quotationService.selectLxr(khName);
    }


    /**
     * 对应机会列表
     * @param khName
     * @return
     */
    @RequestMapping("/sales-list")
    @ResponseBody
    public List<Sales> selectSales(String khName){
        return quotationService.selectSales(khName);
    }

    /**
     * 需要报价的产品
     * @param salessId
     * @return
     */
    @RequestMapping("/pur-list")
    @ResponseBody
    public List<Salesdetails> selectSalesDetails(Integer salessId){
        return quotationService.selectProductList(salessId);
    }

    /**
     * 查询已签合同的报价单不能删除
     * @param quotationId
     * @return
     */
    @RequestMapping("/selectHt")
    @ResponseBody
    public Quotation selectHt(Integer quotationId){
        return quotationService.selectHt(quotationId);
    }

    /**
     * 查询需要打印的报价单
     * @param quotationId
     * @param name
     * @return
     */
    @RequestMapping("/selectContacts")
    @ResponseBody
    public Quotation selectContacts(Integer quotationId,String name){
       return quotationService.selectContacts(quotationId,name);
    }

    /**
     * 销售机会页面已生成报价单不可删除
     * @param salesId
     * @return
     */
    @RequestMapping("/selectSalesQuotation")
    @ResponseBody
    public List<Quotation> selectSalesQuotation(Integer salesId){
        //System.out.println("联系人值"+khName);
        return quotationService.selectSalesQuotation(salesId);
    }

}
